/////////////////////////////////////////////////////////////////
//File: CategoryInfo.cs
//Description: 
//  Ajax 代码
//  
//Copyright© 2011 Moliu
//History:
//  2011-3-23 22:16:15    CodeSmith5.0.1    Original Version
/////////////////////////////////////////////////////////////////

using System;
using System.Threading;
using System.Collections.Generic;
using System.IO;
using MAF.Common;
using My.Blog.Domain.Entities;
using My.Blog.Domain.Interfaces;
using My.Blog.Business.Service;
using My.Blog.Business.Security;
using My.Blog.Web.Controllers;
using System.Web;
using System.Configuration;
using System.Collections.Specialized;

namespace My.Blog.Web.UI
{
    public class CategoryList : My.Blog.Web.Controllers.BasePage
    {
        // 创建用于普通列表显示的节点
        public string NodeForList(CategoryInfo info, int curIndex)
        {
            if (0 == curIndex)
            {
                return "";
            }
            return string.Format(
                "{2}<a href='{4}/ArticleList.aspx?CategoryId={0}'>{1}</a> {3}"
                , info.Id
                , info.Title
                , (true == this.IsMasterMode) ? string.Format("<input name='Ids' type='checkbox' value='{0}' />", info.Id) : ""
                , (true == this.IsMasterMode) ? string.Format("<a href='#{0}' class='tree-node-edit'>[‖]</a>", info.Id) : ""
                , this.RootUrl
                );
        }

        // 创建专门用来选择的节点(例如：选择父节点)
        private string NodeForSelect(CategoryInfo info, int curIndex)
        {
            if (0 == curIndex)
            {
                info.Title = "<span class='root'>根目录</span>";
            }
            return string.Format(
                "<a href='#{0}'>{1}</a>"
                , info.Id
                , info.Title
                );
        }

        // 创建专门用来选择的节点，不包含根节点
        private string NodeForSelectWithoutRoot(CategoryInfo info, int curIndex)
        {
            if (0 == curIndex)
            {
                return "";
            }
            return string.Format(
                "<a href='#{0}'>{1}</a>"
                , info.Id
                , info.Title
                );
        }

        public CategoryList()
        {
            HttpContext context = HttpContext.Current;

            // 标签
            const string IF_EMPTY = "If:Empty";
            const string IF_NOT_EMPTY = "IfNot:Empty";
            const string IF_MAINTAIN = "If:Maintain";
            //const string LOOP_LIST = "Loop:List";

            // 模板路径及逻辑标签正则表达式
            string TPL_FILE_PATH = ConfigurationManager.AppSettings["TemplatesPath"] + @"/CategoryList.html";
            const string REG_LOGIC = @"(?is)<(Tag>)(.*?)</\1";

            // 模板处理对象
            string tpl = File.ReadAllText(context.Server.MapPath(TPL_FILE_PATH));
            Template tplLogic = new Template(tpl, REG_LOGIC);
            Template tplEntity;

            // 数据源  
            CategoryHandler _CategoryHandler = new CategoryHandler(this);
            IList<CategoryInfo> list = _CategoryHandler.ReadCategoriesByBlogId();

            if (0 == list.Count)
            {
                tplLogic.Replace(IF_NOT_EMPTY, "");
                context.Response.Write(tplLogic.Result);
            }
            else
            {
                tplLogic.Replace(IF_EMPTY, "");

                if (true != this.IsMasterMode)
                    tplLogic.Replace(IF_MAINTAIN, "");

                tplEntity = new Template(tplLogic.Result);
                NameValueCollection mapEntity = new NameValueCollection();
                MAF.Common.TreeView<CategoryInfo> tv = new MAF.Common.TreeView<CategoryInfo>(list);

                mapEntity.Add("{RootUrl}", this.RootUrl);
                mapEntity.Add("{TreeViewList}", tv.BuildTree(new MAF.Common.TreeView<CategoryInfo>.TreeNodeContent(NodeForList)));
                mapEntity.Add("{TreeViewSelect}", tv.BuildTree(new MAF.Common.TreeView<CategoryInfo>.TreeNodeContent(NodeForSelect)));
                mapEntity.Add("{TreeViewSelectWithoutRoot}", tv.BuildTree(new MAF.Common.TreeView<CategoryInfo>.TreeNodeContent(NodeForSelectWithoutRoot)));

                tplEntity.Replace(mapEntity);

                context.Response.Write(tplEntity.Result);
            }
        }
    }
}